package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.FansListEntity;
import com.zw.yzk.kabu.data.network.task.GetFansTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.mapper.FansListMapper;
import com.zw.yzk.kabu.view.view.FansWallView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/4.
 */

public class FansWallPresenter implements Presenter {

    private UseCase getFansList;
    private AppCompatActivity activity;
    private FansWallView myFansView;
    private FansListMapper mapper;

    @Inject
    public FansWallPresenter(@Named("fansListUseCase") UseCase getFansList,
                             AppCompatActivity activity, FansListMapper mapper) {
        this.getFansList = getFansList;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(FansWallView view) {
        this.myFansView = view;
    }

    public void getFansList(int ownerId, int start) {
        getFansList.execute(new MyFansListSubscriber(activity), new GetFansTask(ownerId, start));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getFansList.unSubscribe();
    }

    private class MyFansListSubscriber extends BaseSubscriber<FansListEntity> {

        public MyFansListSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(FansListEntity entity) {
            super.onNext(entity);
            myFansView.getFansListSuccess(mapper.transfer(entity));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            myFansView.getFansListFail();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_fans_list_failed);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }
    }
}
